package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IStatementItemService statementItemService;

    @RequiresPermissions("system:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        return statementItemService.query(qo);
    }

    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String editDetail(Model model, Long statementId) {
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        if (statement.getStatus().equals(Statement.STATUS_CONSUME)) {
            //存放到作用域中
            return prefix + "itemAdd";
        }
         //根据statementId 查出 Statement对象 , 通过userService 中的get方法获取到User对象,
        //设置到statement 对象中的 Payee对象中
        statement.setPayee(userService.get(statement.getPayeeId()));
        return prefix + "itemDetail";

    }

    //保存明细功能
    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }
}
